package com.blog.controller;

import com.blog.redis.RedisCache;
import com.blog.redis.RedisUtil;
import com.blog.service.SysCacheService;

import com.blog.util.dto.AjaxResult;
import com.blog.util.laydto.LayDto;
import com.blog.util.laydto.LayDtoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.*;

/**
 * @author cgx
 * @Date 2021/9/29 17:57
 * 系统缓存控制类
 */
@Controller
@RequestMapping("system/cache")
public class SysCacheController {

    @Resource
    RedisUtil redisUtil;

    @Resource
    SysCacheService cacheService;

    /**
     * 获取缓存数据
     * @param
     * @return
     */
    @GetMapping("/getCacheData")
    @ResponseBody
    public LayDto getCacheData(){
        Set<String> keys = redisUtil.keys("*");
        List<String> list = new ArrayList<>(keys);
        List<RedisCache> sysCacheList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            RedisCache redisCache = new RedisCache();
            redisCache.setRedisKey(list.get(i));
            sysCacheList.add(redisCache);
        }
        return LayDtoUtil.returnSuccess(0,"",sysCacheList.size(),sysCacheList);
    }


    /**
     * 查询缓存详情
     * @param
     * @return
     */
    @PostMapping("/getValueByKey")
    @ResponseBody
    public AjaxResult getValueByKey(@PathParam("redisKey") String redisKey) {
        return cacheService.queryCacheValue(redisKey);
    }

    /**
     *
     * @param
     * @return
     */
    @PostMapping("/deleteByKey")
    @ResponseBody
    public AjaxResult deleteByKey(@PathParam("redisKey") String redisKey){
        redisUtil.del(redisKey);
        return AjaxResult.success();
    }

    @GetMapping("/clear")
    @ResponseBody
    public Map<String,Object> clear(){
        Map<String, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("msg","清理成功喽");
        return map;
    }


}
